Skip to content

Conversation

@nxd010
Copy link

@nxd010 nxd010 commented Oct 5, 2025

Description

added a header to each packet with a session ID and sequence number. After the initial broadcast, receivers send NACKs (Negative Acknowledgements) listing any missing packets. The sender then re-broadcasts only those missing chunks, ensuring a complete and ordered file.

To improve flexibility, hardcoded network addresses were moved to a configuration file. The receiver now safely buffers chunks before writing to disk, preventing file corruption from out-of-order packets. This makes the broadcast fast, resilient to packet loss, and stable.

Related Issue

Fixes #3 (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Motivation and Context

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Screenshots (if appropriate):

Checklist:

  • I have registered myself at Contrihub website.
  • My code follows the code style of this project.
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • Any dependent changes have been merged and published in downstream modules
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

@nxd010
Copy link
Author

nxd010 commented Oct 5, 2025

My take on this is I have an Acknowledgment system(NACK) that checks whether all the packets have arrived or not. If not, the system says that these packets [Sequence Number] are not present and again retransmits those missing packets.

@lokesh-wagh
Copy link

could you share a video of this working also can you test this against the tcp based approach to see if your approach actually to quantize the effect of changing to your approach

@nxd010
Copy link
Author

nxd010 commented Oct 6, 2025

I'm sorry, sir I didn't understand

test this against the TCP-based approach

Do I redo this using the TCP approach?
Let me explain my changes and what was implemented before

Sender:

It reads the file piece by piece (BUFFER_SIZE).

For each piece, it prepends a sequence number (0, 1, 2...).

It puts this data into a UDP packet and broadcasts it to everyone on the network.

After the last piece, it sends a final packet with a special sequence number (-1) to signal the end.

Receiver:

It immediately creates and opens the output file.

It listens for packets.

For each packet it receives, it reads the sequence number. If it's not -1, it writes the data chunk directly to the file.

When it sees the -1 packet, it closes the file and stops.

This was the logic before

My Update

Sender:

First, it reads the entire file into memory, splitting it into a list of chunks (slices). This makes it easy to re-send any specific chunk later.

It creates a random session ID for this transfer.

It broadcasts all the chunks one by one (the first pass). Each packet has a detailed header.

After the first pass, it sends a special End-Of-Broadcast (FLAG_EOB) packet.

It then waits and listens for NACK messages from receivers on a separate nackSocket.

If it receives NACKs (e.g., "I'm missing packets 42, 123, and 501 for session 98765"), it re-broadcasts only those specific chunks.

This "listen and repair" cycle repeats for a few rounds to ensure everyone gets the complete file.

Receiver:

It listens for packets and stores them in an in-memory map, using the sequence number as the key. This automatically handles out-of-order packets.

When it receives the FLAG_EOB packet, it checks its map to see if it has all the chunks from 0 to totalPackets.

If chunks are missing, it constructs and sends a NACK message to the sender, listing the sequence numbers of the missing chunks. It then waits for the re-transmissions.

If all chunks are present, it loops from 0 to totalPackets, retrieves the chunks from its map in the correct order, writes them to the output file, and closes it.

I will also share a Video shortly, but sir, how do I simulate packet loss?

@lokesh-wagh
Copy link

@nxd010 i meant could you implement a basic tcp based broadcast and check if using the udp approach makes things better??

@nxd010
Copy link
Author

nxd010 commented Oct 6, 2025

ok sir

@nxd010
Copy link
Author

nxd010 commented Oct 7, 2025

@lokesh-wagh sir, I tried using basic TCP, but it will defeat the scope of these changes since TCP is a One-to-one connection, which means it can't be used for a one-to-many connection where we need to broadcast files to many peers. However, while digging around, I found out a hybrid method using UDP as a beacon and TCP for Unicast file transfer. I will generate a PR for that after confirming changes

@lokesh-wagh
Copy link

that's the point i want to benchmark your approach against tcp based one and see what works the best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix the BroadCast File Transfer

2 participants